<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kra8\Snowflake\HasSnowflakePrimary;

class GroupPolicy extends Model
{
    use SoftDeletes;
    use HasSnowflakePrimary;
    protected $fillable = [
        'name',
        'user_id',
        'cate',
        'resource_cate'
    ];

    public $incrementing = false;

    public function author()
    {
        return $this->belongsTo('App\Models\User', 'user_id');
    }

    public function rules()
    {
        return $this->hasMany('App\Models\GroupPolicyRule', 'group_policy_id');
    }

    public function getDistributorsAttribute()
    {
        
        return $this->rules()->where('cate','DISTRIBUTOR')->get();
    }

    public function getMerchantsAttribute()
    {
        return $this->rules()->where('cate','MERCHANT')->get();
    }

    public function getStaffsAttribute()
    {
        return $this->rules()->where('cate','STAFF')->get();
    }
}
